package com.zd.common.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;


import java.beans.PropertyVetoException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DButils {

    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.0:3306/javatt";
    private static String user="root";
    private static String password="zhaogoodhao123.";

    public static Connection getConn() throws PropertyVetoException, SQLException {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClass(driver);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        /*获取链接*/
        Connection conn = dataSource.getConnection();
        return  conn;
    }

    /*释放*/
    public  static void close(ResultSet rs, PreparedStatement pstat,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(pstat!=null){
            try {
                pstat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
